function logout() {
	document.getElementById('user_login').innerHTML = '<form><input type="text" id="username"/> <input type="password" id="password"/> <a href="#" onclick="login_request()">Login</a></form>';
	script_request('logout', '');
	logged = 0;
	username= '';
	ssid = 0;
	reload_pane();
}










function script_request(function_type, data){
	//alert("running request funcs.php/?function=" + function_type + data);
	script_remove();
	var s = document.createElement("script");
	//s.src =  "scripttest.js";
	if(function_type == 'add_user')
		s.src = "funcs.php/?function=" + function_type + data;
	else
		s.src = "funcs.php/?function=" + function_type +'&ssid=' + ssid + data;
	s.setAttribute('id','async');
	s.setAttribute('onload','show_me_the_money(), reload_pane()');
	//s.setAttribute('onload','reload_pane()'); <-- not necessary due to function setup; do not uncomment, will cause endless loop =D
	var head = document.getElementsByTagName('head')[0];
	head.appendChild(s);
	first_load = 0;
	//alert('You are about to see listings');
}
function script_remove(){
	//alert('JSON function:'+rsp.function );
	
	var head = document.getElementsByTagName('head')[0];
	var olds = document.getElementById('async');
	if(olds){
		//alert('JS FILE REMOVED'+ document.getElementById('async').src);
		head.removeChild(olds);
	}
}



function login_request() {
	if(document.getElementById('username').value == '' || document.getElementById('password').value == ""){
		alert('Both username and password fields must be filled');
		return -1;
	}
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	}
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = function(){
		//alert("state is " + http_request.readyState);
		if (http_request.readyState == 4) {
			if (http_request.status == 200){
				result = eval("(" + http_request.responseText+ ")");
				if(result['success'] == "true"){
					//alert('user logged in');
					document.getElementById('user_login').innerHTML = "Logged in as " + result['user'];
					document.getElementById('user_login').innerHTML += ' <a Href="#" onclick="logout()">Logout</a>';
					logged = 1;
					if(result['is_admin'] == 1){
						is_admin = "true";
					}
					else{
						is_admin = "false";
					}
					username = result['user'];
					ssid = result['ssid'];
					initialize();
				}
				else {
					document.getElementById('user_login').innerHTML += '<br />' + result['error'];
				}
			}
			else {
				alert('There was a problem with the request.');
			}
		}		
	};
	var salt= get_salt();
	var password=md5_crypt(document.getElementById('password').value, salt);
	var postdata="function=login&username="+document.getElementById('username').value+"&password="+password;
	http_request.open('POST', 'funcs.php', true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", postdata.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(postdata);
}